package com.data.modules.online.config.service.p020a;

import com.data.modules.online.cgform.extend.ExtendJsonKey;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.OnlFormShowType;
import org.apache.commons.lang.StringUtils;
import com.data.modules.online.config.exception.DBException;
import com.data.modules.online.config.executor.ColumnMeta;
import com.data.modules.online.config.executor.DataBaseConst;
import com.data.modules.online.config.service.DbTableHandleI;

public class DbTablePostgresHandleImpl implements DbTableHandleI {
    @Override
    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD COLUMN " + getAddColumnDataType(columnMeta) + ";";
    }

    @Override
    public String getReNameFieldName(ColumnMeta columnMeta) {
        return " RENAME  COLUMN  " + columnMeta.getOldColumnName() + " to " + columnMeta.getColumnName() + ";";
    }

    @Override
    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) throws DBException {
        return "  ALTER  COLUMN   " + getAlterColumnDataType(cgformcolumnMeta, datacolumnMeta) + ";";
    }

    @Override
    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return "  ALTER  COLUMN   " + getAlterColumnDropDefDataType(cgformcolumnMeta, datacolumnMeta) + ";";
    }

    @Override
    public String getMatchClassTypeByDataType(String dataType, int digits) {
        if (dataType.equalsIgnoreCase(DataBaseConst.f477l)) {
            return DataBaseConst.f474i;
        }
        if (dataType.equalsIgnoreCase("double")) {
            return "double";
        }
        if (dataType.contains("int")) {
            return "int";
        }
        if (dataType.equalsIgnoreCase("Date") || dataType.equalsIgnoreCase("timestamp")) {
            return OnlFormShowType.f320d;
        }
        if (dataType.equalsIgnoreCase("bytea")) {
            return "blob";
        }
        if (dataType.equalsIgnoreCase(ExtendJsonKey.f91e)) {
            return ExtendJsonKey.f91e;
        }
        if (!dataType.equalsIgnoreCase(DataBaseConst.f470e) && !dataType.equalsIgnoreCase("numeric")) {
            return "";
        }
        return "bigdecimal";
    }

    @Override
    public String dropTableSQL(String tableName) {
        return " DROP TABLE  " + tableName + " ;";
    }

    @Override
    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }

    /**
     * 修改字段
     * @param aVar
     * @param aVar2
     * @return
     */
    private String getAlterColumnDataType(ColumnMeta aVar, ColumnMeta aVar2) throws DBException {
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            return aVar.getColumnName() + "  type character varying(" + aVar.getColumnSize() + ") ";
        }
        if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            return aVar.getColumnName() + "  type timestamp ";
        }
        if (aVar.getColunmType().equalsIgnoreCase("int")) {
            return aVar.getColumnName() + " type int4 ";
        }
        if (aVar.getColunmType().equalsIgnoreCase("double")) {
            return aVar.getColumnName() + " type  numeric(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") ";
        }
        if (aVar.getColunmType().equalsIgnoreCase("BigDecimal")) {
            return aVar.getColumnName() + " type  decimal(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") ";
        }
        if (aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            return aVar.getColumnName() + " text ";
        }
        if (!aVar.getColunmType().equalsIgnoreCase("blob")) {
            return "";
        }
        throw new DBException("blob类型不可修改");
    }

    /**
     * 修改字段，删除默认值
     * @param aVar
     * @param aVar2
     * @return
     */
    private String getAlterColumnDropDefDataType(ColumnMeta aVar, ColumnMeta aVar2) {
        String str;
        if (aVar.mo1290a(aVar2)) {
            return "";
        }
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            StringBuilder append = new StringBuilder().append(aVar.getColumnName());
            if (StringUtils.isNotEmpty(aVar.getFieldDefault())) {
                str = " SET DEFAULT " + aVar.getFieldDefault();
            } else {
                str = " DROP DEFAULT";
            }
            return append.append(str).toString();
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            return aVar.getColumnName() + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " SET DEFAULT " + aVar.getFieldDefault() : " DROP DEFAULT");
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            return aVar.getColumnName() + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " SET DEFAULT " + aVar.getFieldDefault() : " DROP DEFAULT");
        } else if (aVar.getColunmType().equalsIgnoreCase("double")) {
            return aVar.getColumnName() + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " SET DEFAULT " + aVar.getFieldDefault() : " DROP DEFAULT");
        } else if (aVar.getColunmType().equalsIgnoreCase("bigdecimal")) {
            return aVar.getColumnName() + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " SET DEFAULT " + aVar.getFieldDefault() : " DROP DEFAULT");
        } else if (!aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            return "";
        } else {
            return aVar.getColumnName() + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " SET DEFAULT " + aVar.getFieldDefault() : " DROP DEFAULT");
        }
    }

    private String getAddColumnDataType(ColumnMeta aVar) {
        String str = "";
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            str = aVar.getColumnName() + " character varying(" + aVar.getColumnSize() + ") ";
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            str = aVar.getColumnName() + " timestamp ";
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            str = aVar.getColumnName() + " int4";
        } else if (aVar.getColunmType().equalsIgnoreCase("double")) {
            str = aVar.getColumnName() + " numeric(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") ";
        } else if (aVar.getColunmType().equalsIgnoreCase("bigdecimal")) {
            str = aVar.getColumnName() + " decimal(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") ";
        } else if (aVar.getColunmType().equalsIgnoreCase("blob")) {
            str = aVar.getColumnName() + " bytea(" + aVar.getColumnSize() + ") ";
        } else if (aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            str = aVar.getColumnName() + " text ";
        }
        return str + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " DEFAULT " + aVar.getFieldDefault() : " ");
    }

    private String m458b(ColumnMeta aVar) {
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            return aVar.getColumnName() + " character varying(" + aVar.getColumnSize() + ") ";
        }
        if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            return aVar.getColumnName() + " datetime ";
        }
        if (aVar.getColunmType().equalsIgnoreCase("int")) {
            return aVar.getColumnName() + " int(" + aVar.getColumnSize() + ") ";
        }
        if (aVar.getColunmType().equalsIgnoreCase("double")) {
            return aVar.getColumnName() + " numeric(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") ";
        }
        return "";
    }

    @Override
    public String getCommentSql(ColumnMeta columnMeta) {
        return "COMMENT ON COLUMN " + columnMeta.getTableName() + "." + columnMeta.getColumnName() + " IS '" + columnMeta.getComment() + CgformUtil.f229C;
    }

    @Override
    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName;
    }

    @Override
    public String countIndex(String indexName, String tableName) {
        return "SELECT count(*) FROM pg_indexes WHERE indexname = '" + indexName + "' and tablename = '" + tableName + CgformUtil.f229C;
    }
}
